// components/agora-pusher.js
const Utils = require("../../utils/util.js")

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    url: {
      type: String,
      value: "",
    },
    type: {
      type: String,
      value: "",
    },
    uid: {
      type: String,
      value: "",
    },
    muted: {
      type: Boolean,
      value: false
    },
    enableCamera: {
      type: Boolean,
      value: true
    },
    myInfo: {
      type: Object,
      value: {}
    },
    toggle: {
      type: Boolean,
      value: false
    },
    beauty: {
      type: Number,
      value: 3
    },
    whiteness: {
      type: Number,
      value: 3
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pusherContext: null,
    // detached: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    start() {
      // if (!this.data.detached) {
        this.data.pusherContext.stop();
        this.data.pusherContext.start();
      // }
    },
    stop() {
      this.data.pusherContext.stop();
    },

    rePlay: function() {
      this.data.pusherContext.start();
    },

    switchCamera() {
      this.data.pusherContext.switchCamera();
    },
    stateChange(e) {
      console.log('播放状态改变', e.detail)
    },
    pusherError(e) {
      console.log('推流渲染错误', e,detail)
    },

    /**
     * 推流状态更新回调
     */
    recorderStateChange: function (e) {
      if (e.detail.code === -1307) {
        //re-push
        Utils.log('live-pusher stopped', "error");
        this.setData({
          status: "error"
        })
        //emit event
        this.triggerEvent('pushfailed');
      }

      if (e.detail.code === 1008) {
        //started
        Utils.log(`live-pusher started`);
        if(this.data.status === "loading") {
          this.setData({
            status: "ok"
          })
        }
      }
    },
    recorderNetChange: function(e) {
      Utils.log(`network: ${JSON.stringify(e.detail)}`);
    }
  },

  /**
   * 组件生命周期
   */
  ready: function () {
    this.data.pusherContext || (this.data.pusherContext = wx.createLivePusherContext(this));
    this.data.pusherContext.start()
    console.log(this.data.myInfo)
    console.log(this.data.toggle)
  },
  moved: function () {
  },
  detached: function () {
    console.log('走了1')
    this.data.pusherContext && this.data.pusherContext.stop();
  }
})
